package top.ysh.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {

    /**
     * 提供Jedis的Bean
     */
    @Bean
    // 没有名字为jedis的bean，提供一个；有，使用自定义的
    @ConditionalOnMissingBean(name = "jedis")
    public Jedis jedis(RedisProperties redisProperties){
        // 只能操作指定端口号的redis,写死了，不推荐
        // return new Jedis("192.168.23.129",6379);

        // 配置类动态获取
        System.out.println("RedisAutoConfiguration...");
        return new Jedis(redisProperties.getHost(),redisProperties.getPort());
    }
}
